package beans;

import java.util.ArrayList;
import java.util.List;

public class SprintViewBean {
	private List<SprintItemBean> sprintList;
	
	public SprintViewBean() {
		sprintList = new ArrayList<SprintItemBean>();
	}
	
	// checks if sprint with matching ID is in the list
	private SprintItemBean sprintInList(SprintItemBean sprintItemBean) {
		for (SprintItemBean i : sprintList) {
			if (i.getId() == sprintItemBean.getId()) {
				return i;
			}
		}
		
		return null;
	}
	
	public void addSprintItem(SprintItemBean sprintItemBean) {
		// if sprint id exists in the list, then just add the user story
		// else add the SprintItemBean to the list
		SprintItemBean listItem;
		if ((listItem = sprintInList(sprintItemBean)) != null) {
			for (UserStoryBean userStory : sprintItemBean.getUserStories()) {
				listItem.addUserStory(userStory);
			}
		}
		else {
			sprintList.add(sprintItemBean);
		}
		
	}

	public List<SprintItemBean> getList() {
		List<SprintItemBean> newList = new ArrayList<SprintItemBean>(sprintList);
		
		return newList;
	}
	
	public String getJson() {
		StringBuffer sb = new StringBuffer();

		sb.append("{ ");
		sb.append("\"row\": [");
		for (int i = 0; i < sprintList.size(); i++) {
			SprintItemBean sprintItem = sprintList.get(i);
			sb.append(sprintItem.getJson());

			if (i != sprintList.size() - 1) {
				sb.append(",");
			}
		}
		sb.append("]");
		sb.append("}");
		
		return sb.toString();
	}

}
